<template>
	<view class="page">
		<u-navbar bgColor="#FFFFFF" title="我的预约" placeholder leftIconColor="#00000000" :titleStyle="titleStyle">
		</u-navbar>
		<view style="flex-direction: column; display: flex;">
			<view class="topV">
				<view class="view1">
					<view class="tv1">{{userInfo.train_days}}</view>
					<view class="tv2">累计天数</view>
				</view>
				<image :src="userInfo.avatar"></image>
				<view class="view1">
					<view class="tv1">{{userInfo.train_nums}}</view>
					<view class="tv2">累计训练/次</view>
				</view>
			</view>
			<view style="margin-top: 60rpx;"></view>

			<u-sticky zIndex="99" :offsetTop="statusBarHeight+44" bgColor="#FFFFFF">
				<u-tabs :list="tabList" lineColor="#E6A5B4" :scrollable="false" :activeStyle="activeStyle"
					:inactiveStyle="inactiveStyle" @click="onSelectClick" />
			</u-sticky>
			<view class="line1"></view>
			<view v-if="dataList.length>0">
				<scroll-view style="height: 70vh;" :scroll-y="true" @scroll="onScroll" :scroll-with-animation="true"
					:refresher-enabled="true" :refresher-triggered="triggered" @refresherrefresh="onDataList"
					refresher-default-style="white" @scrolltolower="scrolltolower">
					<view class="itemV" v-for="(item,key) in dataList" :key="key" @tap="onItemClick(item)">
						<image :src="item.coach_avatar"></image>
						<view class="itemCenterV">
							<view class="tv1">{{item.title}}</view>
							<view class="tv2">{{item.type_text}}</view>
							<view class="tv3">{{item.time}}</view>
						</view>
						<view v-if="item.status==0" class="itemTv">{{item.status_text}}</view>
						<view v-if="item.status!=0" class="itemTvTo">{{item.status_text}}</view>
					</view>
				</scroll-view>
			</view>

			<view v-if="dataList.length==0"
				style="justify-content: center; align-items: center; display: flex; height: 50vh; color: #8D8D8D;">
				暂无数据
			</view>
		</view>
		<ns-login ref="login" @saveUserInfo="onUserInfoClick"></ns-login>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				triggered: false,
				statusBarHeight: 20,
				titleStyle: {
					"color": "#ffffff"
				},
				activeStyle: {
					"color": "#E6A5B4D",
					"font-size": "34rpx"
				},
				inactiveStyle: {
					"color": "#8D8D8D",
					"font-size": "32rpx"
				},
				tabList: [{
					name: '全部' //-1
				}, {
					name: '待服务' //0
				}, {
					name: '已确认' //1
				}, {
					name: '已取消' //2
				}],
				dataList: [],
				mPage: 1,
				mStatus: -1,
				scrollTop: 0,
				userInfo: {},
			}
		},
		onLoad() {
			uni.getSystemInfo({
				success: res => {
					this.statusBarHeight = res.statusBarHeight
				}
			})
			this.getSubscribeList();
		},
		onShow() {
			this.userInfo = uni.getStorageSync("user_info");

			if (this.userInfo) {
				this.$refs.login.close();
			} else {
				this.$refs.login.open({
					show: false
				});
			}
		},
		methods: {
			onUserInfoClick(e) {
				if (e.code == 200) {
					this.userInfo = uni.getStorageSync("user_info");
				}
			},
			onDataList() {
				if (this.scrollTop == 0) {
					this.triggered = true;
					this.mPage = 1
					this.getSubscribeList()
				}
			},
			scrolltolower() { //上拉加载
				this.mPage++
				this.getSubscribeList()
			},
			onScroll(e) {
				this.scrollTop = e.detail.scrollTop
			},
			getSubscribeList() {
				this.$api.getSubscribeList({
					status: this.mStatus == -1 ? "" : this.mStatus,
					page: this.mPage
				}).then((res) => {
					if (this.mPage == 1) {
						this.dataList = res.list;
					} else {
						this.dataList.push(...res.list)
					}
					this.triggered = false;
				}).catch((e) => {
					this.triggered = false;
				})
			},
			onSelectClick(e) {
				let index = e.name;
				if (index == "全部") {
					this.mPage = 1;
					this.mStatus = -1;
					this.getSubscribeList();
				} else if (index == "待服务") {
					this.mPage = 1;
					this.mStatus = 0;
					this.getSubscribeList();
				} else if (index == "已确认") {
					this.mPage = 1;
					this.mStatus = 1;
					this.getSubscribeList();
				} else if (index == "已取消") {
					this.mPage = 1;
					this.mStatus = 2;
					this.getSubscribeList();
				}
			},
			onItemClick(item) {
				this.$u.route({
					url: '/pages/schedule/BookingDetails',
					params: {
						id: item.id,
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.topV {
		margin-top: 20rpx;
		display: flex;
		align-items: center;
		justify-content: center;

		.view1 {
			flex: 1;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;

			.tv1 {
				color: #E6A5B4;
				font-size: 70rpx;
				font-weight: bold;
			}

			.tv2 {
				color: #828282;
				font-size: 28rpx;
				margin-top: 2rpx;
			}
		}

		image {
			width: 150rpx;
			height: 150rpx;
			border-radius: 50%;
		}
	}

	.line1 {
		height: 1rpx;
		margin-left: 30rpx;
		margin-right: 30rpx;
		background-color: #383838;
	}

	.itemV {
		margin-left: 30rpx;
		margin-right: 30rpx;
		height: 201rpx;
		border-radius: 10rpx;
		background: #1F1F1F;
		margin-top: 30rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;

		.itemCenterV {
			width: 300rpx;
			max-width: 300rpx;
			min-width: 300rpx;
			height: 120rpx;
			justify-content: space-between;
			flex: 1;
			display: flex;
			flex-direction: column;
			margin-left: 20rpx;
			margin-right: 20rpx;

			.tv1 {
				font-size: 28rpx;
				color: #E8E8E8;
				flex: 1;
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
			}

			.tv2 {
				font-size: 24rpx;
				color: #E6A5B4;
				flex: 1;
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
			}

			.tv3 {
				font-size: 24rpx;
				color: #E8E8E8;
				flex: 1;
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
			}
		}

		.itemTv {
			width: 130rpx;
			max-width: 130rpx;
			min-width: 130rpx;
			background: #E6A5B4;
			border: 1px solid #E6A5B4;
			border-radius: 10rpx;
			color: #333;
			font-size: 28rpx;
			margin-top: 40rpx;
			padding-top: 10rpx;
			padding-bottom: 10rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			margin-right: 20rpx;
		}

		.itemTvTo {
			width: 130rpx;
			max-width: 130rpx;
			min-width: 130rpx;
			border-radius: 10rpx;
			border: 1px solid #939393;
			color: #939393;
			font-size: 28rpx;
			margin-top: 40rpx;
			padding-top: 10rpx;
			padding-bottom: 10rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			margin-right: 20rpx;
		}

		image {
			width: 120rpx;
			height: 120rpx;
			min-width: 120rpx;
			border-radius: 50%;
			margin-left: 20rpx;
		}
	}
</style>
